/*
 * Copyright 2016 Yan Zhenjie
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mingmen.canting.caigourenyuan;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

import com.lzy.imagepicker.ImagePicker;
import com.lzy.imagepicker.bean.ImageItem;
import com.lzy.imagepicker.ui.ImageGridActivity;
import com.lzy.imagepicker.view.CropImageView;
import com.mingmen.canting.R;
import com.mingmen.canting.base.BaseBean;
import com.mingmen.canting.dialog.ButtomDialogView;
import com.mingmen.canting.http.callback.StringCallback;
import com.mingmen.canting.httpconfig.HttpResponse;
import com.mingmen.canting.imageloader.GlideImageLoader;
import com.mingmen.canting.model.PicSendModel;
import com.mingmen.canting.ratingbar.BaseRatingBar;
import com.mingmen.canting.ratingbar.RotationRatingBar;
import com.mingmen.canting.util.GsonUtil;
import com.mingmen.canting.util.SharePreferenceUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;
import okhttp3.Call;
import okhttp3.Response;


/**
 * <p>
 * ListView形式的侧滑菜单，支持横向和竖向的。
 * </p>
 * Created by Yan Zhenjie on 2016/7/27.
 */
public class CaiGouPingJiaActivity extends Activity {
    @BindView(R.id.img_return)
    ImageView imgReturn;

    @BindView(R.id.layout_close)
    LinearLayout layoutClose;

    @BindView(R.id.starView)
    RatingBar starView;
    @BindView(R.id.star_one)
    RotationRatingBar star_one;
    private ImagePicker imagePicker;
    @BindView(R.id.img_select)
    ImageView img_select;
    ArrayList<ImageItem> images = null;
    @BindView(R.id.img_good)
    ImageView img_good;
    @BindView(R.id.text_good)
    TextView text_good;
    @BindView(R.id.img_zhong)
    ImageView img_zhong;
    @BindView(R.id.text_zhong)
    TextView text_zhong;
    @BindView(R.id.img_bad)
    ImageView img_bad;
    @BindView(R.id.text_bad)
    TextView text_bad;
    @BindView(R.id.checkboxing)
    CheckBox checkboxing;
    String anonymousFlag = "1";
    @BindView(R.id.star_two)
    RotationRatingBar star_two;
    @BindView(R.id.star_three)
    RotationRatingBar star_three;
    float rationgone, rationgtwo, rationgthree;
    @BindView(R.id.submit_comment)
    Button submit_comment;
    String billId;
    @BindView(R.id.edit_bianxie)
    EditText edit_bianxie;
    String path;

    List<File> myfile = new ArrayList<>();
    File file;
    String token;
    @BindView(R.id.layout_haoping)
    LinearLayout layout_haoping;
    @BindView(R.id.layout_zhong)
    LinearLayout layout_zhong;
    @BindView(R.id.layout_chaping)
    LinearLayout layout_chaping;
    String pictureUrl = "";
    ButtomDialogView bottomSheetDialog;
    public static final int REQUEST_CODE_SELECT = 100;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_chushizhang_new);
        setContentView(R.layout.activity_chushizhang);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
        createPicker();
        token = (String) SharePreferenceUtil.get(this, "token", "");
        billId = getIntent().getStringExtra("billId");
        createDialog();
        star_one.setOnRatingChangeListener(new BaseRatingBar.OnRatingChangeListener() {
            @Override
            public void onRatingChange(BaseRatingBar ratingBar, float rating, boolean fromUser) {
                Log.d("CaiGouPingjia", "BaseRatingBar onRatingChange: " + rating);
                rationgone = rating;
            }
        });
        rationgone = 5;
        star_one.setRating(5);

        star_two.setOnRatingChangeListener(new BaseRatingBar.OnRatingChangeListener() {
            @Override
            public void onRatingChange(BaseRatingBar ratingBar, float rating, boolean fromUser) {
                Log.d("CaiGouPingjia", "BaseRatingBar onRatingChange: " + rating);
                rationgtwo = rating;
            }
        });
        rationgtwo = 5;
        star_two.setRating(5);
        star_three.setOnRatingChangeListener(new BaseRatingBar.OnRatingChangeListener() {
            @Override
            public void onRatingChange(BaseRatingBar ratingBar, float rating, boolean fromUser) {
                Log.d("CaiGouPingjia", "BaseRatingBar onRatingChange: " + rating);
                rationgthree = rating;
            }
        });
        rationgthree = 5;
        star_three.setRating(5);
        checkboxing.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    anonymousFlag = "0";
                } else {
                    anonymousFlag = "1";
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }

    public void createPicker() {
        imagePicker = ImagePicker.getInstance();

        imagePicker.setImageLoader(new GlideImageLoader());
//        imagePicker.setMultiMode(true);
        imagePicker.setMultiMode(false);
        imagePicker.setShowCamera(true);
        imagePicker.setCrop(false);
        imagePicker.setSaveRectangle(true);
        imagePicker.setStyle(CropImageView.Style.RECTANGLE);
        Integer width = Integer.valueOf(280);
        Integer height = Integer.valueOf(280);
        width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, width, getResources().getDisplayMetrics());
        height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, height, getResources().getDisplayMetrics());
        imagePicker.setFocusWidth(width);
        imagePicker.setFocusHeight(height);
        imagePicker.setOutPutX(Integer.valueOf(800));
        imagePicker.setOutPutY(Integer.valueOf(800));
    }

    int goodflag = 0;

    @OnClick({R.id.img_return, R.id.layout_close, R.id.img_select, R.id.img_good, R.id.text_good, R.id.img_zhong, R.id.text_zhong, R.id.img_bad, R.id.text_bad, R.id.submit_comment, R.id.layout_haoping, R.id.layout_zhong})
    public void onViewClicked(View view) {
        switch (view.getId()) {

            case R.id.layout_close:
            case R.id.img_return:
                finish();
                break;
            case R.id.img_select:
                bottomSheetDialog.show();
                break;
            case R.id.img_good:
            case R.id.text_good:
            case R.id.layout_haoping:
                goodflag = 0;
                text_good.setTextColor(getResources().getColor(R.color.red_textcolor));
                text_zhong.setTextColor(getResources().getColor(R.color.grey_color3));
                text_bad.setTextColor(getResources().getColor(R.color.grey_color3));
                img_good.setImageResource(R.mipmap.commentgood_select);
                img_zhong.setImageResource(R.mipmap.pingjiazhong_unselect);
                img_bad.setImageResource(R.mipmap.chaping_unselect);
                break;
            case R.id.img_zhong:
            case R.id.text_zhong:
            case R.id.layout_zhong:
                goodflag = 1;
                text_good.setTextColor(getResources().getColor(R.color.grey_color3));
                text_zhong.setTextColor(getResources().getColor(R.color.red_textcolor));
                text_bad.setTextColor(getResources().getColor(R.color.grey_color3));
                img_good.setImageResource(R.mipmap.commentgood_unselect);
                img_zhong.setImageResource(R.mipmap.pingjiazhong_select);
                img_bad.setImageResource(R.mipmap.chaping_unselect);
                break;
            case R.id.img_bad:
            case R.id.text_bad:
            case R.id.layout_chaping:
                goodflag = 2;
                text_good.setTextColor(getResources().getColor(R.color.grey_color3));
                text_zhong.setTextColor(getResources().getColor(R.color.grey_color3));
                text_bad.setTextColor(getResources().getColor(R.color.red_textcolor));
                img_good.setImageResource(R.mipmap.commentgood_unselect);
                img_zhong.setImageResource(R.mipmap.pingjiazhong_unselect);
                img_bad.setImageResource(R.mipmap.chaping_select);
                break;
            case R.id.submit_comment:
                HttpResponse.setEvaluation(token, billId, goodflag + "", edit_bianxie.getText().toString(), pictureUrl, anonymousFlag, String.valueOf(rationgone), String.valueOf(rationgtwo), String.valueOf(rationgthree), new StringCallback(this) {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        BaseBean baseBean = new BaseBean();
                        baseBean = GsonUtil.GsonToBean(s, BaseBean.class);
                        if (baseBean.getCode() == 0) {
                            EventBus.getDefault().post(5);
                            finish();
                        }
                        Toast.makeText(CaiGouPingJiaActivity.this, baseBean.getMessage() + "", Toast.LENGTH_SHORT).show();
                    }
                });
                break;
        }
    }

    List<String> pictureList = new ArrayList<>();

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == ImagePicker.RESULT_CODE_ITEMS) {
            if (data != null && requestCode == 100) {
                images = (ArrayList<ImageItem>) data.getSerializableExtra(ImagePicker.EXTRA_RESULT_ITEMS);
                for (int i = 0; i < images.size(); i++) {
                    Log.d("dkdkdkk", images.get(i).path);
                    path = images.get(i).path;
                    file = new File(path);
                    myfile.add(file);
                }
                File file = new File(path);
//                File file = new File(path);
                Log.d("Myfile", myfile.toString());
                HttpResponse.pictureSend("123", myfile, new StringCallback(CaiGouPingJiaActivity.this) {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        Log.d("DKkdkdkd", s);
                        PicSendModel picSendModel = new PicSendModel();
                        picSendModel = GsonUtil.GsonToBean(s, PicSendModel.class);
                        pictureList = picSendModel.getData();
                        pictureUrl = picSendModel.getData().get(0);
                        myfile.clear();
                        images.clear();
                    }
                });
//.params("file", multple)
           /*     for (int i = 0; i < myfile.size(); i++) {
                    File multple = myfile.get(i);
                    MyOkhttp.post("http://192.168.0.18:9001/system/upload/img")
                            .params("token", token)
                            .params("file", multple)
                            .execute(new StringCallback(this) {
                                @Override
                                public void onSuccess(String s, Call call, Response response) {
                                    Log.d("DKkdkdkd", s);
                                }
                            });
                }*/

                Bitmap bm = BitmapFactory.decodeFile(images.get(0).path);
                img_select.setImageBitmap(bm);
            } else {
                Toast.makeText(this, "没有数据", Toast.LENGTH_SHORT).show();
            }
        }
    }

    public void onEventMainThread(Integer a) {


    }


    public void createDialog() {
        LayoutInflater layoutInflater = LayoutInflater.from(CaiGouPingJiaActivity.this);
        View view1 = layoutInflater.inflate(R.layout.dialog_bottom_send, null);

        bottomSheetDialog = new ButtomDialogView(CaiGouPingJiaActivity.this, view1, true, true);


        TextView text_dismiss = view1.findViewById(R.id.text_dismiss);
        text_dismiss.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bottomSheetDialog.dismiss();
            }
        });

        ImageView img_camera = view1.findViewById(R.id.img_camera);
        img_camera.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(CaiGouPingJiaActivity.this, ImageGridActivity.class);
                intent.putExtra(ImageGridActivity.EXTRAS_TAKE_PICKERS, true); // 是否是直接打开相机
                startActivityForResult(intent, REQUEST_CODE_SELECT);
                bottomSheetDialog.dismiss();
            }
        });

        ImageView img_calumb = view1.findViewById(R.id.img_calumb);
        img_calumb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(CaiGouPingJiaActivity.this, ImageGridActivity.class);
                intent.putExtra(ImageGridActivity.EXTRAS_IMAGES, images);

                startActivityForResult(intent, 100);
                bottomSheetDialog.dismiss();
            }
        });
    }

}